package com.info3k.client.commands.user;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.info3k.client.Info3K;
import com.info3k.client.components.ContentBox;
import com.info3k.client.components.InfoWindow;
import com.info3k.client.components.validators.TextBoxValidator;
import com.info3k.client.domain.User;
import com.info3k.client.enums.TabTypes;
import com.info3k.client.enums.UserTypes;


public class EditUserCommand extends ContentBox implements Command {

	private TextBoxValidator username = new TextBoxValidator();
	private TextBoxValidator email = new TextBoxValidator();
	private TextBox name = new TextBox();
	private TextBox surname = new TextBox();
	private TextBox phone = new TextBox();
	private ListBox userType = new ListBox();
	
	private Button editButton = new Button(Info3K.msgs.save());
	private Button changePassword = new Button(Info3K.msgs.changePassword());
	
	private Button addPermissionsButton = new Button(Info3K.msgs.chooseUserPermissions() + "[0]");
	
	private Info3K info3k;
	
	private EditUserCommand parentChild;
	
	private User user;

	public EditUserCommand(Info3K info, User us){
		this.info3k = info;
		this.user = us;
		parentChild = this;
		content.setWidth("100%");
		
		for (UserTypes type : UserTypes.values()) {
			userType.addItem(type.toString(), type.toString());
			if(user.getUserType()!=null && user.getUserType().toString().equals(type.toString())){
				userType.setSelectedIndex(userType.getItemCount()-1);
			}
		}
		
		username.setWidth("80%");
		email.setWidth("80%");
		name.setWidth("80%");
		surname.setWidth("80%");
		phone.setWidth("80%");
		userType.setWidth("80%");
		userType.setEnabled(info3k.getLoginManager().getUser().getUserType().equals(UserTypes.ADMINISTRATOR));
		
		if(user.getUsername()!=null){
			username.setText(user.getUsername());
			username.setValid(true);
			username.setEnabled(false);
		}
		if(user.getEmail()!=null){
			email.setText(user.getEmail());
			email.setValid(true);
		}
		
		if(user.getName()!=null){
			name.setText(user.getName());
		}
		
		if(user.getSurname()!=null){
			surname.setText(user.getSurname());
		}
		
		if(user.getPhone()!=null){
			phone.setText(user.getPhone());
		}
		
		editButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent e) {
				if(username.isValid() && email.isValid()){
					user.setEmail(email.getText());
					user.setPhone(phone.getText());
					user.setSurname(surname.getText());
					user.setName(name.getText());
					if(info3k.getLoginManager().getUser().getUserType().equals(UserTypes.ADMINISTRATOR))
						user.setUserType(UserTypes.valueOf(userType.getValue(userType.getSelectedIndex())));
					
					Info3K.loader.setVisible(true);
					Info3K.infoService.addUser(user, new AsyncCallback<Void>(){
						public void onFailure(Throwable caught) {
							Info3K.loader.setVisible(false);
							InfoWindow.showMessage(Info3K.msgs.error(), caught.getMessage());
						}
	
						public void onSuccess(Void result) {
							Info3K.loader.setVisible(false);
							InfoWindow.showMessage(Info3K.msgs.ok(), Info3K.msgs.userEditedSuccessfully());
							info3k.getTabManager().refresh(TabTypes.LATEST_USERS_LEFT);
							info3k.getTabManager().refresh(TabTypes.LATEST_USERS_TOP);
							hide();
						}
					});
				} else InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.pleaseFillAllFields());
			}
		});
		
		if(info3k.getLoginManager().getUser().getUsername().equals(user.getUsername())){
			setText(Info3K.msgs.settings());
		} else {
			setText(Info3K.msgs.editUser());
		}
		
		final Label lblUsername = new Label(Info3K.msgs.username());
		final Label lblEmail = new Label(Info3K.msgs.email());
		final Label lblName = new Label(Info3K.msgs.uName());
		final Label lblSurname = new Label(Info3K.msgs.surname());
		final Label lblPhone = new Label(Info3K.msgs.phone());
		final Label lblUserType = new Label(Info3K.msgs.userType());
		
		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setWidth("100%");
		VerticalPanel vPanel1 = new VerticalPanel();
		VerticalPanel vPanel2 = new VerticalPanel();
		vPanel1.setWidth("100%");
		vPanel2.setWidth("100%");
		content.add(hPanel);
		hPanel.add(vPanel1);
		hPanel.add(vPanel2);
		
		vPanel1.add(lblUsername);
		vPanel1.add(username);
		vPanel1.add(lblUserType);
		vPanel1.add(userType);
		vPanel1.add(lblEmail);
		vPanel1.add(email);
		vPanel2.add(lblName);
		vPanel2.add(name);
		vPanel2.add(lblSurname);
		vPanel2.add(surname);
		vPanel2.add(lblPhone);
		vPanel2.add(phone);
		
		email.addChangeHandler(new ChangeHandler(){
			public void onChange(ChangeEvent event) {
				email.setValid(email.getText().length()>0);
			}
		});
		
		changePassword.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				ChangePasswordCommand cmd = new ChangePasswordCommand(info3k, parentChild, user);
				cmd.execute();
			}
		});
		
		if(info3k.getLoginManager().getUser()!=null && info3k.getLoginManager().getUser().getUsername().equals(user.getUsername())){
			footer.add(changePassword);
			changePassword.getElement().getStyle().setProperty("margin", "0 10px 0 0");
		}
		footer.add(editButton);
	}

	public void execute() {
		center();
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

}